/***********************************************
 This file is part of redia-lib-utils-ios.
 
 Copyright (c) 2014 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

#import "RediaMutableURLRequest.h"

@implementation RediaMutableURLRequest


- (void)multipartAddJPEGImage:(UIImage *)image
{
    static int filecounter=1;
    [self multipartAddJPEGImage:image name:@"uploaded-file" filename:[NSString stringWithFormat:@"%d.jpg",filecounter++]];
}

-(void)multipartAddJPEGImage:(UIImage *)image name:(NSString *)name filename:(NSString*)filename
{
    [self multipartAddData:UIImageJPEGRepresentation(image, 0.9) contentType:@"image/jpeg" name:name filename:filename];
}

- (void)multipartAddPNGImage:(UIImage *)image
{
    static int filecounter=1;
    [self multipartAddPNGImage:image name:@"uploaded-file" filename:[NSString stringWithFormat:@"%d.png",filecounter++]];
}

-(void)multipartAddPNGImage:(UIImage *)image name:(NSString *)name filename:(NSString *)filename
{
    [self multipartAddData:UIImagePNGRepresentation(image) contentType:@"image/png" name:name filename:filename];
}

-(void)multipartAddString:(NSString *)string name:(NSString *)name
{
    [self multipartAddData:[string dataUsingEncoding:NSUTF8StringEncoding] contentType:nil name:name filename:nil];
}

- (NSMutableData *)multipartBody
{
    if (_multipartBody == nil) {
        _multipartBody = [NSMutableData new];
    }
    return _multipartBody;
}

- (NSString *)multipartBoundary
{
    if (_multipartBoundary == nil) {
        _multipartBoundary = [[NSUUID UUID] UUIDString];
    }
    return _multipartBoundary;
}

- (void)multipartAddData:(NSData *)data contentType:(NSString *)contentType name:(NSString*)name filename:(NSString*)filename
{
    [self.multipartBody appendData:[[NSString stringWithFormat:@"--%@\r\n", self.multipartBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    if (filename!=nil) {
        [self.multipartBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename] dataUsingEncoding:NSUTF8StringEncoding]];
    } else {
        [self.multipartBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    if (contentType!=nil) {
        [self.multipartBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
    } else {
        [self.multipartBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    [self.multipartBody appendData:data];
    [self.multipartBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", self.multipartBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}

- (void)multipartFinish
{
    [self setHTTPMethod:@"POST"];
    [self addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", self.multipartBoundary] forHTTPHeaderField:@"Content-Type"];
    //DLog(@"body: %@",self.multipartBody);
    [self setHTTPBody:self.multipartBody];
}

@end
